package com.cxps.server.handler;

import com.alibaba.fastjson.JSON;
import com.cxps.cloud.message.ApiResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 无权限处理类
 *
 * @author YouCai.Liu
 * @since 2021/10/24
 * ...
 */
public class AuthNoPermissionHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exception) throws IOException, ServletException {
        System.out.println("No Permission： " + exception.getMessage());
        ApiResponse<Authentication> apiResponse = new ApiResponse<>();
        apiResponse.setCode("2300");
        apiResponse.setMsg("无权访问");
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(apiResponse));
    }
}